import { wxRequest } from '../../../utils/server.js';
import cmsinfo_share from "../../../component/painter/palette/cmsinfo";
const cmsinfoShare = new cmsinfo_share()
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    pageNo: 1,
    list: [],
    hasNextPage: false,
    keyword: '',
    provelist:[],
    provId:'',
    address: [],
    itemdetail:{},
    pageData: {}
  },

  onShow: function () {
    this.getlist() 
  },

  //查询列表
  getlist: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/protected/info/findAll',
      method: 'POST',
      data: {
        keyword: this.data.keyword,
        pageNo: this.data.pageNo,
        disId: this.data.address.length > 2 ? this.data.address[2].code : '',
        cityId: this.data.address.length > 1 ? this.data.address[1].code : '',
        proId: this.data.address.length > 0 ? this.data.address[0].code : ''
      }
    }, (res) => {
      let list = res.list;
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }

    
      this.setData({
        list: list,
        pageData: res.page,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });

      console.log(res)

    })
  },


  //下拉刷新
  onPullDownRefresh: function () {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },

  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },

  //搜索关键词
  seachInputConfirm: function (e) {
    this.setData({
      pageNo: 1,
      keyword: e.detail.value
    })
    this.getlist();
  },
  seachBtn: function () {
    this.setData({
      pageNo: 1,
      keyword: this.data.keyword
    })
    this.getlist();
  },
  seachInputVal: function (e) {
    var inputval = e.detail.value;
    console.log(e)
    this.setData({
      keyword: inputval
    })
  },

  //清空
  clearInput: function () {
    this.setData({
      keyword: ''
    })
    this.getlist();
  },



  //编辑
  handleEdit: function (e) {
    var item = e.currentTarget.dataset.item
    // wx.setStorageSync('newsItem', item)
  
    wx.navigateTo({
      url: '/pages/publish/cmsinfo/index?id=' + item.id,
    })
  },


  //删除
  handlDelet: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: '确定删除该信息',
      success: function (res) {
        if (res.confirm) {
          that.delectRequest(id)
        }
      }
    })
  },


  delectRequest: function (id) {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + "/sys/protected/info/delete",
      method: 'POST',
      data: { id }
    }, (res) => {
      console.log(res)
      wx.showToast({
        title: "删除成功",
      })
      that.getlist()
    })
  },



  //********************筛选更多********************
  popupRight: function () {
    this.setData({
      showPopup: true
    })
  },

  popupCancel: function () {
    this.setData({
      showPopup: false
    })
  },
  //重置
  popupReset: function () {
    this.setData({
      disId: '',
      address: [],
      quyu: false
    })
  },


  popupConfirm: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
    this.popupCancel();
  },

  chooseAddress: function () {
    this.setData({
      quyu: true
    })
  },
  addresscancel: function () {
    this.setData({
      quyu: false
    })
  },

  addressDetail: function (e) {
    this.setData({
      address: e.detail,
      quyu: false
    })
  },

  //********************筛选更多end********************//

  // getProvince: function () {
  //   var that = this
  //   _wxRequest.wxAjax({
  //     url: app.globalData.apiUrl + '/sys/district/findByPid',
  //     method: "post",
  //     data: {
  //       pid: 0,
  //       pageSize:1000
  //     }
  //   }, function (res) {
  //     var list = res.list;  
  //     that.setData({
  //       provelist: list
  //     })
  //   })
  // },

  // handlProveItem: function (e) {
  //   var code = e.currentTarget.dataset.code;
  //   console.log(code,'code')
  //   this.setData({
  //     provId: code,
  //     proveflag:false
  //   })
  //   this.getlist()
  // },

//获取短码encodeShort
  decodeShort(e) {
    var c_id = e.currentTarget.dataset.id;
    var item = e.currentTarget.dataset.item
    this.setData({
      itemdetail: item
    })
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/comm/encodeShort',
      method: 'POST',
      data: {
        longCode: 'id=' + c_id + '&empUserId=' + wx.getStorageSync('userInfo').id
      }

    }, (res) => {
      this.getCode(res.shortCode)
    })
  },

  //获取二维吗
  getCode(scene) {
    var that = this
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/odr/mall/getWxaCodeUnlimit',
      contentType: "application/x-www-form-urlencoded",
      method: 'POST',
      data: {
        page: '/pages/activity/detail',
        width: 750,
        scene
      }
    }, (res) => { 
      that.setData({
        qrCode: res.url
      }, () => {
        that.toShare()
      })
    })
  },

   //分享
  toShare() {
    var qrCode = this.data.qrCode
    this.setData({
      shareImgTemplate: cmsinfoShare.palette(qrCode, this.data.itemdetail, 0)
    });
    wx.showLoading({
      title: "努力生成中..."
    });
  },

  //图片生成完成后呈现在页面上
  onShareImgOK(e) {
    this.setData({
      shareImgUrl: e.detail.path
    });
    wx.hideLoading();
    this.previewShareImg();
  },
  previewShareImg(e) {
    let url = this.data.shareImgUrl;
    wx.previewImage({
      current: url,
      urls: [url]
    });
  },

})